package net.zhengxinyun.performance.controller.check;

import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.CheckEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.CheckFilter;
import net.zhengxinyun.performance.service.Check.CheckService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2018/12/20.
 */
@RestController
@RequestMapping("/check")
public class CheckController {

    @Value("${word-local}")
    private String wordLocal;
    @Value("${word-down}")
    private String wordDown;
    @Value("${word-down-url}")
    private String wordDownUrl;

    private TokenService tokenService;
    private CheckService checkService;
    private LoginService loginService;

    @Autowired
    public CheckController(TokenService tokenService, CheckService checkService, LoginService loginService) {
        this.tokenService = tokenService;
        this.checkService = checkService;
        this.loginService = loginService;
    }

    @PostMapping("/send")
    public Result<String> send(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            String str = checkService.insert(param.checkEntity, userEntity);
            if (!"发送成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @RequestMapping("/reply")
    public Result<String> reply(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            String str = checkService.reply(param.checkEntity, userEntity);
            if (!"回复成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            List<CheckFilter> list = checkService.query(param.checkFilter, userEntity);
            if (list.size() == 0) {
                param.checkFilter.setPageNumber(param.checkFilter.getPageNumber() - 1);
                list = checkService.query(param.checkFilter, userEntity);
            }
            Double pageTotal = Util.intGetTotalPage(checkService.queryForCount(param.checkFilter), param.checkFilter.getPageSize());
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("pageTotal", pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/queryForEmail")
    public Result<ArrayList<CheckFilter>> queryForEmail(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            ArrayList<CheckFilter> list = checkService.queryForMail(param.checkFilter, userEntity);
            if (list == null || list.size() == 0) {
                return ResultUtils.getResults(Result.Status.ERROR, "系统出错，请重试", null);
            }

            return ResultUtils.getSuccessResultList(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/setPoint")
    public Result<String> setPoint(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            String str = checkService.setPoint(param.checkEntity, userEntity);
            if (!"设置成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/downloadWord")
    public synchronized Result<String> downloadWord(@RequestBody CheckParam param) {
        String tmpFile = wordLocal;
        String expFile = wordDown;
        Map<String, String> datas = new HashMap<String, String>();
        UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
        List<CheckFilter> list = checkService.queryForMail(param.checkFilter, userEntity);
        for (CheckFilter filter : list) {
            if (0 == filter.getSerialNumber()) {
                if (StringUtils.isNotBlank(filter.getTitle())) {
                    datas.put("title", filter.getTitle());
                }else {
                    datas.put("title","");
                }
                if (StringUtils.isNotBlank(filter.getAddressee())) {
                    datas.put("addressee", filter.getAddressee());
                }else {
                    datas.put("addressee","");
                }
                if (StringUtils.isNotBlank(filter.getGmtCreate())) {
                    datas.put("gmtCreate", filter.getGmtCreate());
                }else {
                    datas.put("gmtCreate","");
                }
                if (StringUtils.isNotBlank(filter.getStatus())) {
                    switch (filter.getStatus()) {
                        case "0":
                            datas.put("status", "未读");
                            break;
                        case "1":
                            datas.put("status", "已读");
                            break;
                        case "2":
                            datas.put("status", "未审核");
                            break;
                        case "3":
                            datas.put("status", "未评分");
                            break;
                        case "4":
                            datas.put("status", "回退");
                            break;
                        case "5":
                            datas.put("status", "办结");
                            break;
                        case "6":
                            datas.put("status", "逾期");
                            break;
                    }
                }
                if (StringUtils.isNotBlank(filter.getEndTime())) {
                    datas.put("endTime", filter.getEndTime());
                }else {
                    datas.put("endTime","");
                }
                if (StringUtils.isNotBlank(filter.getContent())) {
                    datas.put("content", filter.getContent());
                }else {
                    datas.put("content","");
                }
                String s = filter.getPoint() + "";
                if (!"null".equals(s)) {
                    datas.put("point", filter.getPoint().toString());
                }else {
                    datas.put("point","");
                }
            }
            if (1 == filter.getSerialNumber()) {
                if (StringUtils.isNotBlank(filter.getContent())) {
                    datas.put("replyContent", filter.getContent());
                }else {
                    datas.put("replyContent","");
                }
            }
        }
//        datas.put("title", "标题部份");
//        datas.put("content", "这里是内容，测试使用POI导出到Word的内容！");
//        datas.put("author", "知识林");
//        datas.put("url", "http://www.zslin.com");
        try {
            checkService.build(new File(tmpFile), datas, expFile);
        } catch (Exception e) {
            return ResultUtils.getResults(Result.Status.ERROR, "导出失败，请重试");
        }
        return ResultUtils.getSuccessResults(wordDownUrl);
    }

    @PostMapping("/pass")
    public Result<String> pass(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"2".equals(param.checkEntity.getStatus())) {
                return ResultUtils.getResults(Result.Status.ERROR, "当前状态不可审核");
            }
            if (!"responsible".equals(userEntity.getUserPermissions())) {
                return ResultUtils.getResults(Result.Status.ERROR, "您没有权限做该操作");
            }
            String str = checkService.pass(param.checkEntity);
            if (!"审核成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/noPass")
    public Result<String> noPass(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"2".equals(param.checkEntity.getStatus())) {
                return ResultUtils.getResults(Result.Status.ERROR, "当前状态不可审核");
            }
            if (!"responsible".equals(userEntity.getUserPermissions())) {
                return ResultUtils.getResults(Result.Status.ERROR, "您没有权限做该操作");
            }
            String str = checkService.noPass(param.checkEntity);
            if (!"回退成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = checkService.delete(param.checkEntity);
            if (!"删除成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = checkService.update(param.checkEntity);
            if (!"修改成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/deleteForFile")
    public Result<String> deleteForFile(@RequestBody CheckParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = checkService.deleteForFile(param.checkEntity);
            if (!"删除成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

}

class CheckParam {
    public CheckEntity checkEntity;
    public CheckFilter checkFilter;
    public TokenEntity tokenEntity;
}
